var sliderWidth = 96; // 需要设置slider的宽度，用于计算中间位置

Page({
  data: {
    tabs: ["本公司车辆", "非本公司车辆"],
    activeIndex: "0",
    sliderOffset: 0,
    sliderLeft: 0,

    listFirst: [],
    listFirstSearch: [],
    listSecond: [],

    isadd: false,
    isupdate: false
  },

  onLoad: function () {
    this.loadlist();
  },

  onShow: function () {
    if (this.data.isadd) {
      setTimeout(function () {
        wx.showToast({
          title: '添加成功',
          icon: 'succes',
          duration: 2000,
          mask: true
        })
      }, 2000);
      this.loadlist()

      this.setData({
        isadd: false
      })
    }

    if (this.data.isupdate) {
      setTimeout(function () {
        wx.showToast({
          title: '修改成功',
          icon: 'succes',
          duration: 2000,
          mask: true
        })
      }, 2000);
      this.loadlist()

      this.setData({
        isupdate: false
      })
    }
  },

  loadlist: function () {
    var that = this;
    var userInfo = wx.getStorageSync('userInfo');
    console.log(userInfo)

    that.data.listFirst = []
    that.data.listFirstSearch = []

    wx.getSystemInfo({
      success: function (res) {
        that.setData({
          sliderLeft: (res.windowWidth / that.data.tabs.length - sliderWidth) / 2
        });
      }
    });

    wx.showToast({
      title: '加载中...',
      icon: 'loading',
      duration: 10000
    });

    wx.request({
      url: 'https://www.lanshuicar.com/Server/CarCompany/CarCompOrderVehicles.ashx',
      data: {
        judge: 1,
        id: userInfo.ZID,
        type: 1,
        pagesize: 2000,
        pageindex: 1
      },
      method: 'GET', // OPTIONS, GET, HEAD, POST, PUT, DELETE, TRACE, CONNECT
      // header: {}, // 设置请求的 header
      success: function (res) {
        // success
        wx.hideToast();
        console.log(res)
        var code = res.data.code;
        if (code == 0) {
          that.setData({
            listFirst: that.data.listFirst.concat(res.data.data),
            listFirstSearch: that.data.listFirst.concat(res.data.data),
          })
        }
      },
      fail: function () {
        // fail
      },
      complete: function () {
        // complete
      }
    })
  },

  tabClick: function (e) {
    this.setData({
      sliderOffset: e.currentTarget.offsetLeft,
      activeIndex: e.currentTarget.id
    });
  },

  cancelcar: function (e) {
    var that = this;
    var userInfo = wx.getStorageSync('userInfo');
    console.log(e)
    wx.showModal({
      title: '提示',
      content: '确定删除该车辆？',
      success: function (res) {
        if (res.confirm) {
          wx.request({
            url: 'https://www.lanshuicar.com/Server/AddCar.ashx',
            data: {
              id: userInfo.ZID,
              carid: e.currentTarget.id,
              method: 4,
              type: 1
            },
            method: 'GET', // OPTIONS, GET, HEAD, POST, PUT, DELETE, TRACE, CONNECT
            // header: {}, // 设置请求的 header
            success: function (res) {
              // success
              console.log(res)
              var code = res.data.code;
              //删除成功
              if (code == 0) {
                that.loadlist();
              }
            },
            fail: function () {
              // fail
            },
            complete: function () {
              // complete
            }
          })
        } else {
          console.log('用户点击取消')
        }
      }
    })
  },

  goadd: function () {
    wx.navigateTo({
      url: '../../car_company/car_manage/car_manage_add?pagetype=add',
    })
  },

  inputsearchAdd: function (e) {
    var that = this
    var arr = []
    var keyWord = e.detail.value

    for (var i = 0; i < that.data.listFirstSearch.length; i++) {

      if (that.data.listFirstSearch[i].Cph.indexOf(keyWord) >= 0 || that.data.listFirstSearch[i].sCount.indexOf(keyWord) >= 0) {
        arr.push(that.data.listFirstSearch[i]);
      }
    }
    if (keyWord != null && keyWord != undefined && keyWord != '' && keyWord != ' ' && keyWord.length != 0) {
      that.setData({
        listFirst: arr,
      });
    } else {
      that.setData({
        listFirst: that.data.listFirstSearch
      });
    }
  },

  // cph: that.data.carname,
  // CarType: that.data.chexing,
  // Color: that.data.color,
  // SCount: that.data.seatcount,
  updatecarmsg: function (e) {
    console.log(e)
    wx.navigateTo({
      url: '../../car_company/car_manage/car_manage_add?cph=' + e.currentTarget.dataset.carname + '&cartype=' + e.currentTarget.dataset.cartype + '&color=' + e.currentTarget.dataset.color + '&seat=' + e.currentTarget.dataset.seat + '&pagetype=update&carid=' + e.currentTarget.dataset.carid,
    })
  }
});